package com.zywl.utils.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FaceUtils {
	/**
	 * 登录
	 * @return
	 * @throws Exception
	 */
	public static String login() throws Exception {
		URL url = new URL(
				"http://41.246.144.3:11180/business/api/login");
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setDoOutput(true);
		connection.setDoInput(true);
		connection.setUseCaches(false);
		connection.setInstanceFollowRedirects(true);
		connection.setRequestMethod("POST");
		connection.setRequestProperty("Content-Type",
				"application/json;charset=UTF-8");
		connection.connect();
		OutputStreamWriter out = new OutputStreamWriter(
				connection.getOutputStream(), "UTF-8");
		Map<String,String> bean = new HashMap<String,String>();
		bean.put("name", "user_qzlq");
		bean.put("password", GenerateCodeUtil.MD5("user_qzlq").toLowerCase());
		String json= JSON.toJSONString(bean);
		out.write(json);
		out.flush();
		out.close();
		String strLine = "";
		String strResponse = "";
		InputStream in = connection.getInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
		while ((strLine = reader.readLine()) != null) {
			strResponse += strLine + "\n";
		}
		System.out.println(strResponse);
		reader.close();
		connection.disconnect();
		JSONObject result = JSONObject.parseObject(strResponse)	;
		return result.getString("session_id");

	}


	/**
	 * 1比1接口
	 * @param sessionId
	 * @param json
	 * @return
	 * @throws Exception
	 */
	public static Map<String,Object> faceVerify(String sessionId, String json) throws Exception {
		URL url = new URL(
				"http://41.246.144.3:11180/business/api/face/verify");
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setDoOutput(true);
		connection.setDoInput(true);
		connection.setUseCaches(false);
		connection.setInstanceFollowRedirects(true);
		connection.setRequestMethod("POST");
		connection.setRequestProperty("Content-Type",
				"application/json;charset=UTF-8");
		connection.setRequestProperty("session_id",
				sessionId);
		connection.connect();
		OutputStreamWriter out = new OutputStreamWriter(
				connection.getOutputStream(), "UTF-8");
		out.write(json);
		out.flush();
		out.close();
		String strLine = "";
		String strResponse = "";
		InputStream in = connection.getInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
		while ((strLine = reader.readLine()) != null) {
			strResponse += strLine + "\n";
		}
		System.out.println(strResponse);
		reader.close();
		connection.disconnect();
		JSONObject jsonObject = JSONObject.parseObject(strResponse)	;
		int rtn =jsonObject.getInteger("rtn");
		List<String> list = new ArrayList<String>();
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap.put("rtn",rtn);
		if(rtn==0){
			resultMap.put("rtn",rtn);
			resultMap.put("similarity",jsonObject.getDoubleValue("similarity"));
		}
		 return  resultMap;

	}


}
